package com.unidt.www.module.third.workbar.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.unidt.www.module.third.api.workbar.dto.MessageBody;
import com.unidt.www.module.third.workbar.vo.AccessToken;
import com.unidt.www.module.third.workbar.vo.WorkBarDept;
import com.unidt.www.module.third.workbar.vo.WorkBarUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * @Author sk
 * @Description:
 * @Date 2022/11/17 13:37
 **/
@Slf4j
@Component
public class WorkBarUtils {

    @Autowired
    AppConfig appConfig;
    /**
     * 获取 AccessToken
     *
     * @return
     */
    public AccessToken getAccessToken() {

        String requestUrl = this.appConfig.getAccessTokenUrl();

        log.info("requestUrl:{}",requestUrl);
        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(mediaType);

        Map<String,String > param = new HashMap<>();
        param.put("appKey",appConfig.getAppKey());
        param.put("appSecret",appConfig.getAppSecret());

        HttpEntity<String> request = new HttpEntity<String>(JSON.toJSONString(param), headers);

        RestTemplate restTemplate =new RestTemplate();
        JSONObject jsonObject =  restTemplate.postForObject(requestUrl,request,JSONObject.class);

        log.info("getAccessToken 返回结果:{}",jsonObject.toString());
        if(Objects.nonNull(jsonObject)){

            Integer code = jsonObject.getInteger("code");

            String type = jsonObject.getString("type");
            if(code==200 && type.equals("success")){

                JSONObject data = jsonObject.getJSONObject("data");

                AccessToken accessTokenVo = data.toJavaObject(AccessToken.class);

                log.error("accessToken:{}",accessTokenVo);

                return  accessTokenVo;
            }

        }else {
            log.error("getAccessToken 调用失败 为返回任何信息");
        }
        return null;
    }


    public  List<WorkBarDept> getDeptScopes(AccessToken accessTokenVo) {

        String requestUrl = this.appConfig.getDeptScopeUrl();

        log.info("requestUrl:{}",requestUrl);
        HttpHeaders headers = RequestUtils.getBaseHeader("access_token",accessTokenVo.getAccessToken());

        HttpEntity<String> request = new HttpEntity<String>(headers);

        RestTemplate restTemplate =RequestUtils.getRestTemplate();
        JSONObject jsonObject =  restTemplate.postForObject(requestUrl,request,JSONObject.class);

        log.info("getAccessToken 返回结果",jsonObject.toString());
        if(Objects.nonNull(jsonObject)){

            Integer code = jsonObject.getInteger("code");

            String type = jsonObject.getString("type");
            if(code==200 && type.equals("success")){

                JSONArray data = jsonObject.getJSONArray("data");

                List<WorkBarDept> deptVOS =  data.toJavaList(WorkBarDept.class);

                return deptVOS;
            }


        }else {
            log.error("获取部门失败 调用失败 为返回任何信息");
        }
        return null;
    }

    /**
     * 功能描述: 工作台发送消息
     * @author sk
     * @date 2022/11/29
     * @param messageBody
     * @return java.lang.Boolean
     */

    public  Boolean  sendMessage(MessageBody messageBody){
        AccessToken accessTokenVo = getAccessToken();
        String requestUrl = this.appConfig.getSendNotifyUrl();

        log.info("requestUrl:{}",requestUrl);
        HttpHeaders headers = RequestUtils.getBaseHeader("access_token",accessTokenVo.getAccessToken());

        String paramsJson = JSON.toJSONString(messageBody);
        log.info("paramsJson:{}",paramsJson);
        HttpEntity<String> request = new HttpEntity<String>(paramsJson,headers);

        RestTemplate restTemplate =RequestUtils.getRestTemplate();
        JSONObject jsonObject =  restTemplate.postForObject(requestUrl,request,JSONObject.class);

        log.info("send message 返回结果:{}",jsonObject.toString());
        if(Objects.nonNull(jsonObject)){

            Integer code = jsonObject.getInteger("code");
            String type = jsonObject.getString("type");
            if(code.equals(200) && type.equals("success")){
                    return true;
            }
        }else {
            log.error("发送消息失败");
        }
        return false;

    }


    public static void main(String[] args) {

    }

    public  List<WorkBarDept> getChildDepts(AccessToken accessTokenVo, WorkBarDept deptVO) {

        String requestUrl = this.appConfig.getChildDept();

        log.info("requestUrl:{}",requestUrl);
        HttpHeaders headers = RequestUtils.getBaseHeader("access_token",accessTokenVo.getAccessToken());

        Map<String,Object> param = new HashMap<>();

        List<String> deptIds = new ArrayList<>();
        deptIds.add(deptVO.getId());

        param.put("unitIds",deptIds);

        HttpEntity<String> request = new HttpEntity<String>(JSON.toJSONString(param),headers);

        RestTemplate restTemplate =RequestUtils.getRestTemplate();
        JSONObject jsonObject =  restTemplate.postForObject(requestUrl,request,JSONObject.class);

        log.info("getAccessToken 返回结果",jsonObject.toString());
        if(Objects.nonNull(jsonObject)){

            Integer code = jsonObject.getInteger("code");

            String type = jsonObject.getString("type");
            if(code==200 && type.equals("success")){

                JSONArray data = jsonObject.getJSONArray("data");

                List<WorkBarDept> deptVOS =  data.toJavaList(WorkBarDept.class);

                return deptVOS;
            }


        }else {
            log.error("获取部门失败 调用失败 为返回任何信息");
        }
        return null;
    }


   public WorkBarUser getUserByCode(AccessToken accessToken, String authCode){
        String requestUrl = appConfig.getUserByAuthCode();

        log.info("requestUrl:{}",requestUrl);
        HttpHeaders headers = RequestUtils.getBaseHeader("access_token",accessToken.getAccessToken());

        Map<String,Object> param = new HashMap<>();


        param.put("authCode",authCode);

        HttpEntity<String> request = new HttpEntity<String>(JSON.toJSONString(param),headers);

        RestTemplate restTemplate =RequestUtils.getRestTemplate();
        JSONObject jsonObject =  restTemplate.postForObject(requestUrl,request,JSONObject.class);

        log.info("getUser 返回结果:{}",jsonObject.toString());
        if(Objects.nonNull(jsonObject)){

            Integer code = jsonObject.getInteger("code");

            String type = jsonObject.getString("type");
            if(code==200 && type.equals("success")){

                JSONObject data = jsonObject.getJSONObject("data");

                WorkBarUser user =  data.toJavaObject(WorkBarUser.class);

                return user;
            }


        }else {
            log.error("根据authCode获取用户失败");
        }
        return null;
    }

   /**
    * 功能描述: 处理消息 设置消息为已处理
    * @author sk
    * @date 2022/12/12
    * @param notifyId
    * @return boolean
    */

    public boolean endMessage(String notifyId) {

        AccessToken accessTokenVo = getAccessToken();
        String requestUrl = this.appConfig.getSendNotifyUpdateUrl();

        log.info("requestUrl:{}",requestUrl);
        HttpHeaders headers = RequestUtils.getBaseHeader("access_token",accessTokenVo.getAccessToken());

        Map<String,Object> param  =  new HashMap<>();
        param.put("notifyId",notifyId);
        param.put("processFlag",true);
        String paramsJson = JSON.toJSONString(param);
        log.info("paramsJson:{}",paramsJson);
        HttpEntity<String> request = new HttpEntity<String>(paramsJson,headers);

        RestTemplate restTemplate =RequestUtils.getRestTemplate();
        JSONObject jsonObject =  restTemplate.postForObject(requestUrl,request,JSONObject.class);

        log.info("send message 返回结果:{}",jsonObject.toString());
        if(Objects.nonNull(jsonObject)){

            Integer code = jsonObject.getInteger("code");
            String type = jsonObject.getString("type");
            if(code.equals(200) && type.equals("success")){
                return true;
            }
        }else {
            log.error("发送消息失败");
        }
        return false;
    }
}
